#include "object.h"

CObject::CObject():
m_ID(0),
m_Type(0),
m_pLayer(0),
m_TargetPos(0,0),
m_nSpeedLen(2.0),
m_bFinish(false)
{

}

CObject::~CObject()
{
	//m_pSprite->release();
	m_pLayer->removeChild(m_pSprite,true);
}

bool CObject::Init(const int & id,const int &type,CCLayer* pLayer)
{
	m_ID = id;
	m_Type = type;
	m_pLayer = pLayer;
	return Create();

}

bool CObject::Create()
{
	char* fileName[] = {"Icon-bullet.png","Icon-bullet.png"};
	int size[] = {57,57};
	int nsize = size[m_Type];


	CCSize s = CCDirector::sharedDirector()->getWinSize();

	CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(fileName[m_Type]);

	
	// manually add frames to the frame cache
	CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*0, nsize*0, nsize, nsize));
	CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*1, nsize*0, nsize, nsize));
	//CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*2, nsize*0, nsize, nsize));
	//CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*3, nsize*0, nsize, nsize));
	CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*0, nsize*1, nsize, nsize));
	CCSpriteFrame *frame5 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(nsize*1, nsize*1, nsize, nsize));


	//
	// Animation using Sprite BatchNode
	//
	m_pSprite = CCSprite::spriteWithSpriteFrame(frame0);

	int x = s.width/2;
	int y = 0;
	m_pSprite->setPosition( ccp( x,y) );
	m_pLayer->addChild(m_pSprite);

	CCMutableArray<CCSpriteFrame*> *animFrames = new CCMutableArray<CCSpriteFrame*>(6);
	animFrames->addObject(frame0);
	animFrames->addObject(frame1);
	/*animFrames->addObject(frame2);
	animFrames->addObject(frame3);*/
	animFrames->addObject(frame4);
	animFrames->addObject(frame5);

	CCAnimation *animation = CCAnimation::animationWithFrames(animFrames, 0.2f);
	CCAnimate *animate = CCAnimate::actionWithAnimation(animation, false);
	m_pSprite->runAction(CCRepeatForever::actionWithAction( animate ) );
	animFrames->release();    // win32 : memory leak    2010-0415

	return true;
}

void CObject::SetTargetPos()
{
	CCSize s = CCDirector::sharedDirector()->getWinSize();

	m_TargetPos.x = abs(int((double)rand() / (RAND_MAX + 1) * (s.width+100))) -50;
	m_TargetPos.y = abs(int((double)rand() / (RAND_MAX + 1) * (s.height+100))) -50;

	SetSpeed();
}

void CObject::SetTargetPos(const CCPoint& tarPos)
{
	m_TargetPos= tarPos;
	SetSpeed();
}

void CObject::SetTargetPos(const float& x,const float& y)
{
	m_TargetPos.x = x;
	m_TargetPos.y = y;
	SetSpeed();
}

void CObject::SetSpeed()
{
	CCPoint pos = m_pSprite->getPosition();
	float x = m_TargetPos.x - pos.x;
	float y = m_TargetPos.y - pos.y;

	
	float len = sqrt(x*x + y*y);
	float rate = m_nSpeedLen/len;

	m_nXSpeed = x*rate;
	
	m_nYSpeed =  y*rate;
	

	
}

void CObject::Update(const ccTime &dt)
{
	CCSize s = CCDirector::sharedDirector()->getWinSize();
	CCPoint pos = m_pSprite->getPosition();
	pos.x += m_nXSpeed;
	pos.y += m_nYSpeed;

	if(pos.x < 0 || pos.x > s.width || pos.y < 0 || pos.y > s.height)
	{
		m_bFinish = true;	
		return;
	}
	m_pSprite->setPosition( pos );
}

const CCPoint& CObject::GetPos()
{
	return m_pSprite->getPosition();
}

const CCRect& CObject::GetSize()
{
	return m_pSprite->boundingBox();
}

void	CObject::SetPos(const CCPoint& pos)
{
	m_pSprite->setPosition(pos);
}